package com.disruptor.demo.queue;

import com.disruptor.demo.factory.TestEventFactory;
import com.disruptor.demo.handler.TestDataEventHandler;
import com.disruptor.demo.model.TestData;
import com.disruptor.demo.model.TestDataEvent;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.WorkHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">Mr_He</a>
 * @Copyright (c)</ b> HeC<br/>
 * @createTime 2018/8/2 14:03
 * @Description:
 */
@Component
public class TestDataEventQueueHelper extends BaseQueueHelper<TestData,TestDataEvent,TestDataEventHandler> implements InitializingBean {

    //队列长度
    private static final int QUEUE_SIZE = 1024;

    /**
     * 消费者数量
     */
    @Autowired
    private List<TestDataEventHandler> testDataEventHandlers;

    @Override
    protected int getQueueSize() {
        return QUEUE_SIZE;
    }

    @Override
    protected com.lmax.disruptor.EventFactory eventFactory() {
        return new TestEventFactory();
    }

    @Override
    protected WorkHandler[] getHandler() {
        int size = testDataEventHandlers.size();
        TestDataEventHandler[] paramEventHandlers = (TestDataEventHandler[]) testDataEventHandlers.toArray(new TestDataEventHandler[size]);
        return paramEventHandlers;
    }

    @Override
    protected WaitStrategy getStrategy() {
        return new BlockingWaitStrategy();
    }

    @Override
    public void afterPropertiesSet() {
        this.init("disruptor_test");
    }
}